﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

using AKMII.DMRA.Common;
using AKMII.DMRA.DataAccess;
using AKMII.DMRA.Business.Rule;

namespace AKMII.DMRA.Business.Management
{
    public class ZipManager
    {
        private ZipAccessor zipAccessor;

        public ZipManager()
        {
            zipAccessor = new ZipAccessor();
        }

        public void AddZip(LeadBlock zip)
        {
 
        }

        public List<LeadBlock> GetZipsById(string userId, int month, int year, int type)
        {
            List<LeadBlock> zips = new List<LeadBlock>();

            try
            {
                zips = zipAccessor.GetLeadBlock(userId, month, year, type);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get zips by id", ex);
            }

            return zips;
        }

        public List<LeadBlock> GetZipsById(string userId, int month, int year, string county, List<string> cities)
        {
            List<LeadBlock> zips = new List<LeadBlock>();

            try
            {
                string cityString = string.Empty;

                cityString = cities.Aggregate((a, b) => { return a + "," + b; });

                zips = zipAccessor.GetLeadBlock(userId, month, year, county, cityString);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get zips by id, terr and city", ex);
            }

            return zips;
        }

        public List<SimpleLeadBlock> GetSimpleZipsById(string userId, int month, int year, int type)
        {
            List<SimpleLeadBlock> zips = new List<SimpleLeadBlock>();

            try
            {
                zips = zipAccessor.GetSimpleLeadBlock(userId, month, year, type);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get simple zips by id", ex);
            }

            return zips;
        }

        public List<SimpleLeadBlock> GetSimpleZipsById(string userId, int month, int year, int type, string county, List<string> cities)
        {
            List<SimpleLeadBlock> zips = new List<SimpleLeadBlock>();

            try
            {
                if (cities != null && cities.Count > 0)
                {
                    string cityString = cities.Aggregate((a, b) => { return a + "," + b; });
                    zips = zipAccessor.GetSimpleLeadBlock(userId, month, year, type, county, cityString);
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get simple zips by id, terr, cities", ex);
            }

            return zips;
        }

        public bool AssignLeadBlock(string assignmentName, List<int> zipIds, int month, int year)
        {
            bool result = false;

            try
            {
                StringBuilder allZipIds = new StringBuilder(string.Empty);

                if (zipIds != null && zipIds.Count > 0)
                {
                    for (int i = 0; i < zipIds.Count; i++)
                    {
                        allZipIds.Append(zipIds[i].ToString());

                        if (i != (zipIds.Count - 1))
                        {
                            allZipIds.Append(',');
                        }
                    }
                }

                result = zipAccessor.UpdateZipAssigned(assignmentName,
                    allZipIds.ToString(), month, year, (month == CurrentPeriod.Month && year == CurrentPeriod.Year));
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in assign lead blocks", ex);
                result = false;
            }

            return result;
        }

        public bool AssignZipCode(string assignmentName, string zipName, int month, int year)
        {
            bool result = false;

            try
            {
                List<LeadBlock> leadBlocks = zipAccessor.GetLeadBlock(zipName);
                result = AssignLeadBlock(assignmentName, leadBlocks.Select(c => c.ZipId).ToList<int>(), month, year);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in assign zip code", ex);
                result = false;
            }

            return result;
        }

        public List<LeadBlock> GetZips()
        {
            List<LeadBlock> zips = new List<LeadBlock>();

            try
            {
                zips = zipAccessor.GetLeadBlock();
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get zips", ex);
            }

            return zips;
        }

        public List<ZipCode> GetZipCodes(string mdNumber, int month, int year)
        {
            List<ZipCode> zips = new List<ZipCode>();

            try
            {
                zips = zipAccessor.GetZipCodes(mdNumber, month, year);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get zip codes", ex);
            }

            return zips;
        }

        public LeadBlock GetTypesCount(string year,string month)
        {
            LeadBlock zips = new LeadBlock();
            try
            {
                zips = zipAccessor.GetLeadBlcokTypesCount(year, month);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occours in get zip types count information",ex);
            }
            return zips;
        }
    }
}
